<?php
$content = new Models_Articles();
$news = new Models_News();
$max = 10; //maximum words in query
$minLength = 3; //minimal length of word
$lang = $content->lang;
$errors = $content->searchValidate($_POST["word"], $minLength);
$tagsContent = new Models_Tags();
if($errors) 
{
    foreach ($errors as $error)
    {
        echo "<div class=\"alert alert-danger\"><h4>$error</h4></div>";
    }
} else {
    $word = explode(" ", $content->cleanPostData($_POST["word"]));
    $words = $content->cleanArrayToSearch( $minLength,$word, $max);
    $resultsArticles = $content->searchArticles($words);
    $resultsSubArticles = $content->searchSubArticles($word);
    $countResults = count($resultsSubArticles) + count($resultsArticles);
}
if((count($resultsSubArticles) == 0) and (count($resultsArticles) == 0))
{
    echo '<div class="alert alert-danger"><h4>Nothing found.</h4></div>';
} else {
?>
<script> 
    /* when document is ready */
    $(function() {
        /* initiate plugin */
        $("div.holder").jPages({
            containerID: "itemContainer"
        });
    });
</script>
    <div class="well">
        <h4>Results<span class="badge badge-success"><?php echo $countResults; ?></span>:</h4>
        <hr />
        <ul id="itemContainer" class="list-no-bullet">
        <?php
        foreach ($resultsArticles as $articles)
        {
            $tags = $tagsContent->getArticleTags($articles['id']);
            $countComment = $content->countComment($articles['id']);
            $body = $news->getMiniature($articles['body'], 500);
            $body = strip_tags($body);
             ?>
             <li><div class="well">
                 <h4><a href="/<?php $content->printPath(); ?>articles/show/<?php echo $articles['id'];?>"><?php echo $articles['title']; ?></a></h4>
                 <?php echo $body; ?>
                 <hr />
                <p class="" style="padding: 1px 1px; margin: -5px 0;;">
                      <i class="icon-user"></i> <a href="#">Admin</a> 
                      | <i class="icon-calendar"></i> <?php echo $articles['modified']; ?>
                      | <span class="label label-success"><i class="icon-comment icon-white"></i> <?php echo $countComment['count(id)']; ?></span>
                      | <i class="icon-tags"></i> Tags: 
                      <?php
                        foreach ($tags as $tag)
                        {
                            echo '<a href="/' . $tagsContent->path . 'articles/tag/' . $tag['id'] . '"><span class="label label-info">' . $tag['name'] . '</span></a> ';
                        }
                    ?>
                </p>
             </div></li>
             <?php
        }
        foreach ($resultsSubArticles as $subArticles)
        {
            $tags = $tagsContent->getArticleTags($subArticles['id']);
            $articleId = $content->getArticleId($subArticles['id']);
            $countComment = $content->countComment($articleId[$lang . '_article_id']);
            $body = $news->getMiniature($subArticles['body'], 500);
            $body = strip_tags($body);
             ?>
             <li><div class="well">
                 <h4><a href="/<?php $content->printPath(); ?>articles/showsub/<?php echo $subArticles['id'];?>"><?php echo $subArticles['title']; ?></a></h4>
                 <?php echo $body; ?>
                 <hr />
                <p class="" style="padding: 1px 1px; margin: -5px 0;;">
                      <i class="icon-user"></i> <a href="#">Admin</a> 
                      | <i class="icon-calendar"></i> <?php echo $subArticles['modified']; ?>
                      | <span class="label label-success"><i class="icon-comment icon-white"></i> <?php echo $countComment['count(id)']; ?></span>
                      | <i class="icon-tags"></i> Tags: 
                      <?php
                        foreach ($tags as $tag)
                        {
                            echo '<a href="/' . $tagsContent->path . 'articles/tag/' . $tag['id'] . '"><span class="label label-info">' . $tag['name'] . '</span></a> ';
                        }
                    ?>
                </p>
             </div></li>
             <?php
        }
        ?>
             </ul>
             <!-- navigation panel -->
            <div class="holder pagination">
                <ul>
                    <li><a class="jp-previous jp-disabled">← previous</a></li>
                    <li><a class="jp-current">1</a></li>
                    <li><span class="jp-hidden">...</span></li>
                    <li> <a>2</a></li>
                     <li><a>3</a></li>
                     <li><a>4</a></li>
                     <li><a>5</a></li>
                     <li><a class="jp-hidden">6</a></li>
                     <li><a class="jp-hidden">7</a></li>
                     <li><a class="jp-hidden">8</a></li>
                     <li><a class="jp-hidden">9</a></li>
                     <li><span>...</span></li>
                     <li><a>10</a></li>
                    <li><a class="jp-next">next →</a></li>
                </ul>
            </div>
    </div>    
<?php
}
?>